//
//  BSSelectImage.m
//  GOSProject
//
//  Created by 叶一枫 on 2017/3/20.
//  Copyright © 2017年 yeyifeng. All rights reserved.
//

#import "BSSelectImage.h"
@interface BSSelectImage ()<TZImagePickerControllerDelegate>
@property(nonatomic,strong)UIImagePickerController*imagePicker;
@property(nonatomic,strong)UIImage*selectImage;

@end

@implementation BSSelectImage

+ (BSSelectImage *)defaultBSSelect {
    static BSSelectImage *manager = nil;
    static dispatch_once_t token;
    dispatch_once(&token,^{
        if(manager == nil){
            manager = [[BSSelectImage alloc]init];
        }
    } );
    return manager;
}

-(void)selectImageFromLibrary:(BOOL)fromLibrary type:(NSString *)type viewController:(UIViewController *)viewController{

    if ([type isEqualToString:@"danxuan"]) {
        self.imagePicker = [[UIImagePickerController alloc]init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = YES;
        self.imagePicker.sourceType = fromLibrary?UIImagePickerControllerSourceTypePhotoLibrary:UIImagePickerControllerSourceTypeCamera;
        [viewController presentViewController:self.imagePicker animated:YES completion:nil];
    }else {
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];

        //[imagePickerVc.navigationBar setBackgroundImage:[UIImage imageNamed:@"naviBg.png"] forBarMetrics:UIBarMetricsDefault];
        imagePickerVc.allowPickingVideo = YES;
        imagePickerVc.allowPickingOriginalPhoto = YES;
        [viewController presentViewController:imagePickerVc animated:YES completion:nil];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

    self.selectImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.selectImageBlock(self.selectImage);
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets {
    if (self.selectImagesBlock) {
        self.selectImagesBlock(photos);
    }
}


@end
